package com.party.web.biz.merchantAnalyze;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.core.model.member.MerchantAnalyze;
import com.party.core.model.member.MerchantAnalyzeQuery;
import com.party.core.service.member.IMerchantAnalyzeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.party.common.utils.DateUtils.addMonth;

/**
 * 机构数据分析接口
 * Created by wei.li
 *
 * @date 2017/9/14 0014
 * @time 16:58
 */

@Service
public class MerchantAnalyzeBizService {

    @Autowired
    private IMerchantAnalyzeService merchantAnalyzeService;


    /**
     * 统计详情
     * @param memberId 会员编号
     * @return 详情
     */
    public HashMap<String, MerchantAnalyze> detail(String memberId){
        HashMap<String, MerchantAnalyze> map = Maps.newHashMap();

        String now = DateUtils.todayDate();
        MerchantAnalyzeQuery merchantAnalyzeQuery = new MerchantAnalyzeQuery();
        merchantAnalyzeQuery.setMemberId(memberId);
        merchantAnalyzeQuery.setCreateStart(now);
        merchantAnalyzeQuery.setCreateEnd(now);

        //今日
        MerchantAnalyze today = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //昨日
        String day = DateUtils.formatDate(DateUtils.addDay(now, -1));
        merchantAnalyzeQuery.setCreateStart(day);
        merchantAnalyzeQuery.setCreateEnd(day);
        MerchantAnalyze yesterday = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //前日
        day = DateUtils.formatDate(DateUtils.addDay(now, -2));
        merchantAnalyzeQuery.setCreateStart(day);
        merchantAnalyzeQuery.setCreateEnd(day);
        MerchantAnalyze beforeYDay = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //本周
        merchantAnalyzeQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0)));
        merchantAnalyzeQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfWeek(0)));
        MerchantAnalyze week = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //本月
        merchantAnalyzeQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfMonth(0)));
        merchantAnalyzeQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfMonth(0)));
        MerchantAnalyze month = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //历史
        merchantAnalyzeQuery.setCreateStart(null);
        merchantAnalyzeQuery.setCreateEnd(null);
        MerchantAnalyze history = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        MerchantAnalyze total = new MerchantAnalyze();
        total.setMoney(BigDecimalUtils.round(history.getMoney(), 2));

        map.put("today", today);
        map.put("yesterday", yesterday);
        map.put("beforeYDay", beforeYDay);
        map.put("week", week);
        map.put("month", month);
        map.put("history", history);
        map.put("total", total);
        return map;
    }


    /**
     * 时间列表
     * @param length 长度
     * @return 时间
     */
    public List<Date> dateList(Integer length){
        length = null == length ? 7: length;
        Date now = new Date();
        Date startDate = null;
        if(length == 30){
            startDate = addMonth(DateUtils.getTodayDate(), -1);
        }else{
            startDate = DateUtils.addDay(now, -(length - 1));
        }
        List<Date> dateList =  DateUtils.getDateDifference(startDate, now);
        return dateList;
    }

    /**
     * 时间列表
     * @param length 长度
     * @return 时间
     */
    public List<String> dateStringList(Integer length){
        List<Date> list = dateList(length);
        List<String> dateList = Lists.newArrayList();
        for (Date date : list){
            dateList.add(DateUtils.formatDate(date));
        }
        return dateList;
    }


    /**
     * 时间长度列表
     * @param length 长度
     * @param memberId 会员编号
     * @return 列表数据
     */
    public List<MerchantAnalyze> lengthList(Integer length, String memberId){
        String now = DateUtils.todayDate();
        String startDay = null;
        if(length == 30){
            startDay = DateUtils.formatDate(addMonth(DateUtils.getTodayDate(), -1));
        }else{
            startDay = DateUtils.formatDate(DateUtils.addDay(now, -(length - 1)));
        }
        MerchantAnalyzeQuery merchantAnalyzeQuery = new MerchantAnalyzeQuery();
        merchantAnalyzeQuery.setMemberId(memberId);
        merchantAnalyzeQuery.setCreateStart(startDay);
        merchantAnalyzeQuery.setCreateEnd(now);
        return merchantAnalyzeService.queryList(merchantAnalyzeQuery, null);
    }

    /**
     * 报名列表
     * @param length 长度
     * @param memberId 会员编号
     * @return 报名列表
     */
    public List<String> chartList(Integer length, String memberId, Integer type){
        List<String> applyList = Lists.newArrayList();
        List<MerchantAnalyze> list = this.lengthList(length, memberId);
        List<Date> dateList = this.dateList(length);
        for (Date date : dateList){
            Object num = 0;
            for (MerchantAnalyze merchantAnalyze : list){
                if (DateUtils.formatDate(date).equals(DateUtils.formatDate(merchantAnalyze.getCreateDate()))){
                    if (type == 0){
                        num = merchantAnalyze.getApplyNum();
                    }
                    else if (type == 1){
                        num = merchantAnalyze.getOrderNum();
                    }
                    else if (type == 2){
                        num = merchantAnalyze.getMoney();
                    }
                    else if (type == 3){
                        num = merchantAnalyze.getViewNum();
                    }
                    else if (type == 4){
                        num = merchantAnalyze.getMemberNum();
                    }
                    else {
                        num = merchantAnalyze.getApplyNum();
                    }
                    break;
                }
            }
            applyList.add(num.toString());
        }
        return applyList;
    }
}
